
/***************************************************************************
**
** Copyright 2002 by Hellosoft Inc. All rights reserved. No part
** of this publication, or any software included with it, may be
** reproduced, stored in a retrieval system, or transmitted in
** any form or by any other means, including photocopying,
** electronic or mechanical recording or otherwise, without
** prior written permission of the copyright holder
**
** Hellosoft Confidential and Proprietary information
**
** File			: protocol_engine.h
**
** Start Date	: Fri Jan 4 2002
**
** Email		: wlan-nw@hyd.hellosoft.com
**
***************************************************************************/

#ifndef __PROTOCOL_ENGINE_HEADER__
#define __PROTOCOL_ENGINE_HEADER__

#define MIN_RTS_THRESHOLD                   0
#define MAX_RTS_THRESHOLD                   2346


/* tx_MAC_state - describes the state of the Protocol Engine Transmission */
#define TX_NULL                             0
#define TX_CHANNEL_ACQUIRE_START            1
#define TX_CHANNEL_ACQUIRE_PROGRESS         18
#define TX_NORMAL_STARTED_PREAMBLE          2
#define TX_IMMEDIATE_STARTED_PREAMBLE       3
#define TX_DATA_CFM_WAIT                    4
#define TX_END_CFM_WAIT                     5
#define TX_SUSPEND                          6
#define TX_IMMEDIATE                        7
#define TX_TIME_CFM_WAIT                    8

#define MAC_IN_RECEPTION					9
#define MAC_ACK_WAIT						16
#define MAC_CTS_WAIT						17
#define TX_END_CFM_RECVD					12

#define	MAC_CTS_OR_ACK_WAIT_MASK			16

/* rx_MAC_state - describes the state of the protocol engine receive */
#define RX_NULL                             0
#define CTS_WAIT                            1
#define ACK_WAIT                            2

/* preamble cfm state */
#define PREAMBLE_CFM_NOOP                   0
#define WAITING_FOR_PREAMBLE_CFM            1
#define PREAMBLE_CFM_RECVD                  2

/* data cfm state */
#define DATA_CFM_NOOP                       0
#define WAITING_FOR_DATA_CFM                1
#define DATA_CFM_RECVD                      2

/* txend cfm state */
#define TXEND_CFM_NOOP                      0
#define WAITING_FOR_TXEND_CFM               1
#define TXEND_CFM_RECVD                     2

/* txtime cfm state */
#define TXTIME_CFM_NOOP                     0
#define WAITING_FOR_TXTIME_CFM              1
#define TXTIME_CFM_RECVD                    2

/* PHY indications */
#define RX_STATE_NULL                       0
#define CHANNEL_BUSY_NOT_RECEIVED           0
#define CHANNEL_BUSY_RECEIVED               1
#define RXSTART_INDICATION_CONFIRMED        2
#define DATA_INDICATION_CONFIRMED           3
#define RXEND_INDICATION_NULL               4
#define RXEND_INDICATION_CONFIRMED          5
#define WAITING_FOR_INDICATION              6
#define FREE								0/* sb simulator*/

/* constants used for rx */

#define MAX_PHY_MSG_POOL_SIZE               9  /* max size to recv PHY msg */
#define PHY_TIMER_NULL                      0
#define PHY_TIMER_STARTED                   1
#define TXTIME_REQ_SENT                     1
#define TXTIME_REQ_NOTSENT                  0

#define NOT_TO_SEND                         0
#define TO_SEND                             1

#define MAIN_RET                            0

/* Functions */

/* functions used in protocol engine */
HS_INT32    init_protocol_engine(HS_VOID);              /* initilizes the variables */

HS_VOID     protocol_engine_main(HS_VOID);              /* protocol starts */
HS_VOID     MAC_tx_handler(HS_VOID);                    /* handles the transmission of pkt */

HS_INT32    init_protocol_engine_rx(HS_VOID);           /* protocol engine receive part initialisation */
HS_VOID     MAC_rx_handler(HS_VOID);                    /* handles the reception of packet */

/* funtions called in PHY primitives */
HS_VOID     set_rx_state(HS_UINT32 state);              /* sets the receive state */
HS_UINT32   get_rx_state(HS_VOID);                      /* returns the receive state */
HS_VOID     reset_rx_state(HS_VOID);                    /* resets the receive state */

/* protocol engine rx state functions */
HS_VOID     set_rx_MAC_state(HS_INT32 rx_state);        /* sets the MAC receive state */
HS_VOID     reset_rx_MAC_state(HS_VOID);                /* resets the MAC receive state */
HS_UINT32   get_rx_MAC_state(HS_VOID);                  /* returns the MAC receive state */

/* RXEND_indication functions */
HS_UINT32   get_RXEND_indication_state(HS_VOID);          /* returns the status at the end of reception */
HS_VOID     set_RXEND_indication_state(HS_UINT32 state);  /* sets the RX END Indication state */
HS_VOID     reset_RXEND_indication_state(HS_VOID);        /* resets the RX END Indication state */

/* protocol engine tx state functions */
HS_VOID     set_tx_MAC_state(HS_INT32 tx_state);          /* sets the MAC transmit state */
HS_VOID     reset_tx_MAC_state(HS_VOID);                  /* resets the MAC transmit state */
HS_UINT32   get_tx_MAC_state(HS_VOID);                    /* returns the MAC transmit state */

HS_VOID     reset_preamble_cfm_state(HS_VOID);              /* resets the preamble confirmation state*/
HS_VOID     reset_data_cfm_state(HS_VOID);                  /* resets the data confirmation state */
HS_VOID     reset_txend_cfm_state(HS_VOID);                 /* resets the TX END confirmation state*/

HS_VOID     set_preamble_cfm_state(HS_INT32 preamble_state);    /* sets the preamble confirmation state*/
HS_VOID     set_data_cfm_state(HS_INT32 data_state);            /* sets the data confirmation state */
HS_VOID     set_txend_cfm_state(HS_INT32 txend_state);          /* sets the TX END confirmation state*/

HS_UINT32   get_preamble_cfm_state(HS_VOID);                /* gets the preamble confirmation state*/
HS_UINT32   get_data_cfm_state(HS_VOID);                    /* gets the data confirmation state */
HS_UINT32   get_txend_cfm_state(HS_VOID);                   /* gets the TX END confirmation state*/

HS_UINT32   get_channel_status(HS_VOID);                    /* returns the channel status (BUSY or IDLE) */
HS_VOID     set_channel_status(HS_INT32 state);             /* sets the channel status */

HS_UINT32   get_PHY_pkt_length_info(HS_VOID);
HS_VOID     set_char_recvd(HS_UINT8 recvd_char);
HS_UINT8    get_char_recvd(HS_VOID);

HS_VOID     set_PHY_rx_data_rate(HS_UINT32 rate);           /* sets the physical receive data rate */
HS_UINT32   get_PHY_rx_data_rate(HS_VOID);                  /* returns the Physical receive data rate */

HS_UINT32   get_tx_pkt_type(HS_VOID);                       /* returns the transmit packet type */
HS_VOID     set_tx_pkt_type(HS_UINT32 pkt_type);            /* sets the transmit packet type */
HS_VOID     reset_tx_pkt_type(HS_VOID);                     /* resets the transmit packet type */

HS_UINT16   calculate_CW(HS_UINT16 no_tries);               /* for BACKOFF CALCULATION */

HS_VOID     reset_tx_pkt_length(HS_VOID);                   /* resets the transmit packet length */
HS_VOID     set_tx_pkt_length(HS_UINT32 temp_length);       /* sets the transmit packet length */
HS_UINT32   get_tx_pkt_length(HS_VOID);                     /* returns the transmit packet length */
HS_VOID     decrement_tx_pkt_length(HS_VOID);               /* reduces the transmit packet length for byte by byte transmission */
HS_VOID     reset_tx_pkt_ptr(HS_VOID);                      /* resets the transmit pkt ptr */
HS_VOID     set_tx_pkt_ptr(HS_UINT8 *temp);                 /* sets the transmit packet pointer to the parameter passed */
HS_UINT8    *get_tx_pkt_ptr(HS_VOID);                       /* returns the transmit packet pointer */
HS_VOID     increment_tx_pkt_ptr(HS_VOID);                  /* moves the transmit packet pointer to the next byte */

HS_UINT8    *get_DATA_Immediate_pkt(HS_VOID);               /* returns the immediate data packet to be transmitted */

HS_UINT16    get_immediate_pkt_length(HS_VOID);             /* returns the immediate transmit packet length */
HS_UINT8    *get_immediate_tx_pkt(HS_VOID);                 /* returns the immediate transmit packet */
HS_VOID     set_immediate_tx_pkt(HS_UINT8 *temp_ptr);       /* sets the immediate transmit packet pointer to the parameter passed */
HS_UINT32   get_immediate_tx_pkt_type(HS_VOID);             /* returns the immediate transmit packet type */
HS_VOID     reset_immediate_tx_pkt(HS_VOID);                /* resets the immediate transmit packet type */
HS_UINT16   is_immediate_pkt_ready(HS_VOID);                /* returns if the immediate packet to be transmitted is ready or not */
HS_VOID     set_immediate_tx_pkt_type(HS_UINT16 type);      /* sets the immediate transmit packet type */
HS_VOID     set_immediate_tx_pkt_ptr(HS_UINT8 *pkt_start);  /* sets the immediate transmit packet pointer */
HS_VOID     set_immediate_tx_pkt_length(HS_UINT16 length);  /* sets the immediate transmit packet length */
HS_VOID     set_immediate_pkt_length(HS_UINT16 temp_length);

/* TXTIME CFM functions */
HS_VOID     set_txtime_cfm_state(HS_UINT32 state);      /* sets the transmit time confirmation state to the paramet passed */
HS_VOID     reset_txtime_cfm_state(HS_VOID);            /* resets the transmit time confirmation state */
HS_UINT32   get_txtime_cfm_state(HS_VOID);              /* returns the transmit time confirmation state */

HS_UINT16   get_prev_pkt_more_frag(HS_VOID);                        /* returns the more fragment of the previous packet */

/* functions used in protocol engine rx*/
HS_INT32    create_PHY_msg_pool(HS_VOID);                       /* create memory for the messages received from physical layer */
//HS_INT16    free_PHY_msg(struct PHY2MAC_msg_t *msg_ptr);        /* free the memory for the message received from PHY */
HS_UINT32   is_PHY_msg_available(HS_VOID);                      /* returns if any message is received from the physical layer or not */
//HS_VOID     add_to_PHY_msg_list(struct PHY2MAC_msg_t *to_add);  /* add the message received from physical layer to the messages list */

struct  PHY2MAC_msg_t   *get_PHY_msg(HS_VOID);                  /* returns the message received from the physical layer */
struct  PHY2MAC_msg_t    *alloc_PHY_msg(HS_VOID);               /* allocate memory for the message received from the physical layer */


HS_VOID     process_recvd_pkt(struct PHY2MAC_msg_t *to_process);    /* processes any packet received by the MAC */
//HS_VOID     process_CTS_pkt(CTS_pkt_t recvd_CTS);                   /* process the CTS packet received */
//HS_VOID     process_ACK_pkt(ACK_pkt_t recvd_ACK);                   /* process the ACK packet received */
HS_VOID     process_DATA_pkt(struct PHY2MAC_msg_t *data_pkt);       /* process the DATA packet received */

HS_VOID     CTS_timer_expired_action(HS_VOID);      /* performs action in case of CTS timer expiry */
HS_VOID     ACK_timer_expired_action(HS_VOID);      /* performs action in case of ACK timer expiry */

HS_VOID     set_prev_tx_MAC_state(HS_UINT32 state);     /* sets the state of the previous transmission at MAC */
HS_UINT32   get_prev_tx_MAC_state(HS_VOID);             /* returns the MAC state of the previous transmission */

HS_VOID     process_beacon_for_scanning(struct PHY2MAC_msg_t * PHY2MAC_msg);        /* process the beacon received at
 the time of scanning */
HS_VOID     process_beacon_for_synchronising(struct PHY2MAC_msg_t * PHY2MAC_msg);   /* process the beacon received at the time of joining */
HS_VOID     process_probe_response(struct PHY2MAC_msg_t * PHY2MAC_msg);             /* process the probe response received
in the case of active scanning */

HS_VOID     form_probe_response(Wlan_Addr_t dest_addr);                     /* form the probe response for the probe request received */
HS_VOID     process_AP_data_pkt(struct PHY2MAC_msg_t *AP_data);             /* process the packet destined to AP */
HS_VOID     process_STA_data_pkt_AP(struct PHY2MAC_msg_t *STA_data_in_AP);  /* process the data packet received
by AP from one STA to be delivered to another STA */
HS_VOID     process_STA_data_pkt(struct PHY2MAC_msg_t   *STA_data);            /* process the DATA packet destined to the STA */
HS_VOID     process_DS_data_pkt(struct PHY2MAC_msg_t    *DS_data);             /* process the data packet received by AP
to be delivered to a STA in DS*/

HS_VOID     set_auth_req_flag(HS_INT32 flag);
HS_INT32    get_auth_req_flag(HS_VOID);
HS_VOID     set_auth_req_by_flag(HS_INT32 flag);
HS_INT32    get_auth_req_by_flag(HS_VOID);
HS_VOID     set_auth_req_STA_addr(Wlan_Addr_t temp_addr);
HS_UINT8    *get_auth_req_STA_addr(HS_VOID);
HS_VOID     set_auth_req_by_STA_addr(Wlan_Addr_t temp_addr);
HS_UINT8    *get_auth_req_by_STA_addr(HS_VOID);

HS_VOID     process_deauthentication_req_AP(struct PHY2MAC_msg_t *deauth_req);  /* to process the AP's deauthentication */

HS_VOID     set_associated_STA_addr(Wlan_Addr_t     temp_addr);                 /* setting the address of the associated STA */

HS_UINT8    *get_associated_STA_addr(HS_VOID);                                  /* returns the address of an associated STA */

HS_VOID     set_association_response_flag(
                                            Wlan_Addr_t     to_set,
                                            HS_INT32        state
                                          );   /* sets the association response flag depending on the STA address */

HS_INT32    get_association_response_flag(Wlan_Addr_t to_get);                  /* returns the association response flag
by the STA address */

HS_VOID     set_reassociation_response_flag(
                                                Wlan_Addr_t     to_set,
                                                HS_INT32        state
                                           ); /* sets the reassociation response flag depending on the STA address */

HS_INT32    get_reassociation_response_flag(Wlan_Addr_t to_get);/* returns the association response flag
by the STA address */
HS_INT32    is_association_response_transmitted(HS_VOID);       /* returns if the association response is transmitted or not */
HS_VOID     set_association_response_transmitted(HS_VOID);      /* sets the association response transmitted flag */
HS_VOID     reset_association_response_transmitted(HS_VOID);    /* resets the association response transmitted flag */

//HS_UINT16   get_seq_num(HS_VOID);                              /* gives the sequence number of the mpdu to be transmitted */
#define get_seq_num()  seqno_g                                   /* gives the sequence number of the mpdu to be transmitted */

//HS_VOID     increment_seq_num(HS_VOID);                         /* increments the sequence number */
#define increment_seq_num() (++seqno_g)                           /* increments the sequence number */

HS_VOID     form_PS_poll_frame(HS_VOID);                        /* forms PS POLL to get the buffered messages for the STA */
HS_VOID     process_ATIM(struct PHY2MAC_msg_t * PHY2MAC_msg);   /* process the ATIM received by the STA */

HS_UINT8    *encode_data_in_SAR(
                                    Wlan_Addr_t     temp_source_addr,
                                    Wlan_Addr_t     temp_dest_addr,
                                    HS_UINT8        *data,
                                    HS_UINT16       temp_pkt_length,
                                    HS_UINT16       temp_seqno,
                                    HS_UINT16       temp_frame,
                                    HS_UINT16       temp_more_fr,
                                    HS_UINT16       temp_retry,
                                    HS_UINT16       temp_Privacy,
                                    HS_UINT16       temp_service_class,
                                    HS_UINT16       *temp_length,
				    HS_UINT16       duration_field
                               );  /* encodes data pkt */

HS_UINT8    *encode_mgt_in_SAR(Wlan_Addr_t temp_source_addr,Wlan_Addr_t temp_dest_addr,
                        HS_UINT8 *data, HS_UINT16 temp_pkt_length, HS_UINT16 temp_seqno,
                        HS_UINT16 temp_frame,HS_UINT16 temp_more_fr,HS_UINT16 temp_retry,
                        HS_UINT16 temp_Privacy, HS_UINT16 temp_service_class,HS_UINT16 *temp_length,
                        HS_UINT8 pkt_subtype);

HS_INT32    get_Cord_Fun_type(HS_VOID);                     /* to get the coordination function type */

HS_UINT8    *get_NULL_DATA_pkt_ptr(HS_VOID);                /* returns the ptr to NULL data */
HS_INT32    get_Send_CF_ACK(HS_VOID);                       /* returns the status of CF_ACK pkt sent */
HS_VOID     set_Send_CF_ACK(HS_INT32);                      /* sets the status of CF_ACK pkt send */
HS_INT32    get_access_type(HS_VOID);                       /* returns the coordination function */
HS_VOID     set_access_type(HS_INT32 type);                 /* sets the coordination function */
HS_UINT8    *get_PC_immediate_pkt_ptr(HS_VOID);             /* returns the pointer to the immediate pkt at PC */
HS_VOID     set_PC_immediate_pkt_ptr(HS_UINT8 *temp_ptr);   /* sets the immediate packet pointer at the PC */
HS_UINT16   get_PC_immediate_pkt_length(HS_VOID);           /* returns the immediate packet length at the PC */
HS_VOID     set_PC_immediate_pkt_length(HS_UINT16 );        /* sets the immediate packet length at the PC */
HS_VOID     set_prv_tx_pkt_type(HS_INT32 temp_type);        /* sets the type of the previous transmitted packet */
HS_INT32    get_prv_tx_pkt_type(HS_VOID);                   /* returns the type of the previous transmitted packet */
HS_VOID     reset_prv_tx_pkt_type(HS_VOID);                 /* resets the type of the previous transmitted packet */

HS_VOID     set_tx_PCF_pkt_type(HS_INT32 temp);         /* sets the transmit packet type in CFP */
HS_INT32    get_tx_PCF_pkt_type(HS_VOID);               /* returns the transmit packet type in CFP */
HS_VOID     send_immediate_tx_pkt(HS_VOID);             /* to send the immediate tx. pkt  */

HS_VOID     set_DTIM_is_Transmitted(HS_VOID);           /* sets the DTIM transmitted flag */
HS_INT32    get_DTIM_is_Transmitted(HS_VOID);           /* returns the DTIM transmitted flag */
HS_VOID     reset_DTIM_is_Transmitted(HS_VOID);         /* resets the DTIM transmitted flag */

HS_VOID     set_tx_end_flag(HS_VOID);                   /* flag for sending the TX END */
HS_VOID     reset_tx_end_flag(HS_VOID);                 /* for resetting the TX END flag */
HS_UINT32   get_tx_end_flag(HS_VOID);                   /* returns the TX END flag */

HS_VOID     reset_Null_Func_transmitted(HS_VOID );      /* resets the NULL FUNCTION transmitted flag */
HS_VOID     set_Null_Func_transmitted(HS_VOID );        /* sets the NULL FUNCTION transmitted flag */
HS_INT32    get_Null_Func_transmitted(HS_VOID );        /* returns the NULL FUNCTION transmitted flag */

HS_VOID     process_DATApkt_destSTA(struct PHY2MAC_msg_t *data_pkt);    /* for processing data pkt destine dto the STA */

/* math.h */
HS_UINT32   mac_pow(
                        HS_UINT32 x,
                        HS_UINT32 n
                   );        /* return x^n */

/* For ARM */
HS_VOID     set_beacon_pkt_ready(HS_UINT32 temp_flag);
HS_UINT32   is_beacon_pkt_ready(HS_VOID);
HS_VOID     set_beacon_pkt_length(HS_UINT32 temp_length);
HS_UINT32   get_beacon_pkt_length(HS_VOID);
HS_VOID     set_beacon_pkt_transmitted(HS_UINT32 temp_flag);
HS_UINT32   get_beacon_pkt_transmitted(HS_VOID);

HS_VOID     set_ATIM_pkt_ready(HS_UINT32 temp_flag);
HS_UINT32   is_ATIM_pkt_ready(HS_VOID);
HS_VOID     set_ATIM_pkt_length(HS_UINT32 temp_length);
HS_UINT32   get_ATIM_pkt_length(HS_VOID);
HS_VOID     set_ATIM_pkt_transmitted(HS_UINT32 temp_flag);
HS_UINT32   get_ATIM_pkt_transmitted(HS_VOID);

HS_UINT8    *get_next_CF_tx_pkt(HS_VOID);

HS_UINT16   get_next_PC_pkt_length(HS_VOID);
HS_VOID     set_next_PC_pkt_length(HS_UINT16 temp_length);
HS_VOID     set_next_PC_tx_pkt_type(HS_UINT32 temp_type);
HS_UINT32   get_next_PC_tx_pkt_type(HS_VOID);

HS_UINT8    *get_next_PC_tx_pkt(HS_VOID);
HS_VOID     set_next_PC_tx_pkt(HS_UINT8 *temp_ptr);
HS_UINT32   get_next_PC_tx_pkt_type(HS_VOID);
HS_VOID     reset_next_PC_tx_pkt(HS_VOID);
HS_VOID     set_CF_ACK_Addr(Wlan_Addr_t temp_addr_temp);
HS_VOID     get_CF_ACK_Addr(Wlan_Addr_t temp_addr_temp);
HS_VOID     reset_CF_ACK_Addr(HS_VOID);
HS_UINT8    *get_DPRAM_ptr_for_NULL_DATA_pkt(HS_VOID);
HS_VOID     set_DPRAM_ptr_for_NULL_DATA_pkt(HS_UINT8 *temp_ptr);

HS_VOID     set_STA_access_mode(HS_UINT32 access_mode);
extern HS_UINT32	STA_access_mode_g;
#define     get_STA_access_mode() STA_access_mode_g

HS_INT32	get_RTS_threshold(HS_VOID);
HS_VOID		set_RTS_threshold(HS_INT32 value);

HS_VOID     init_global_variables(HS_VOID);
HS_INT32    init_rx_global_variables(HS_VOID);
HS_VOID     init_pe_global_globals(HS_VOID);

HS_VOID		init_MIB_variables(HS_VOID);

HS_VOID		send_next_immediate_CF_pkt(HS_VOID);

HS_VOID		make_next_CF_tx_pkt(HS_VOID);

HS_VOID		copy_next_normal_pkt_into_DPRAM(HS_VOID);

HS_VOID		copy_next_normal_CFP_pkt_into_DPRAM(HS_VOID);

HS_VOID		unexpected_pkt_recvd_action(HS_INT32 state);

HS_VOID		process_sta_cp_pkt(struct PHY2MAC_msg_t* msg);

HS_VOID		set_beacon_CFP_count_offset (HS_UINT16 count);
HS_UINT16	get_beacon_CFP_count_offset(HS_VOID);

HS_VOID		process_ap_cp_pkt(struct PHY2MAC_msg_t* msg);
HS_VOID		process_ap_recvd_data_pkt(struct PHY2MAC_msg_t	*msg);
HS_VOID		add_msg_to_bridge_module(struct PHY2MAC_msg_t	*msg);

HS_VOID		MAC_ap_cp_tx_handler(HS_VOID);

HS_VOID MAC_ap_cp_tx(HS_VOID);
HS_VOID MAC_sta_cp_tx(HS_UINT32 temp_STA_type_l);

#endif
